package com.cheyunkeji.er.base;

import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.view.Gravity;
import android.view.View;

import com.cheyun.netsalev3.MyApplication;
import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.utils.ScreenAdaptUtil;
import com.cheyunkeji.er.activity.SplashActivity;
import com.cheyunkeji.er.view.SDialog;
import com.cheyunkeji.er.view.SLoadingDialog;
import com.cheyunkeji.er.view.SToast;
import com.gyf.immersionbar.ImmersionBar;
import com.umeng.analytics.MobclickAgent;

import androidx.appcompat.app.AppCompatActivity;


/**
 * Created by Administrator on 2017/2/13.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    protected SLoadingDialog loadDialog ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        MyApplication.getInstance().addAct(this);
        MyApplication.getInstance().addActivity(this);

        //今日头条适配方案
        ScreenAdaptUtil.setCustomDensity(this, getApplication());

        loadDialog = new SLoadingDialog(this);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
//        if (Build.VERSION.SDK_INT >= 23) {
//            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
//            ActivityCompat.requestPermissions(this, mPermissionList, 123);
//        }

        init();
        initView();
        initData();

        if(this.getClass().equals(SplashActivity.class)){
            ImmersionBar.with(this)
                    .fitsSystemWindows(true)
                    .statusBarDarkFont(true)
                    .init();
        }else {
            ImmersionBar.with(this).statusBarColor(R.color.heicolor)
                    .fitsSystemWindows(true)
                    .init();
        }
    }

    protected abstract void init();

    protected abstract void initView();

    protected abstract void initData();

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onDestroy() {
        MyApplication.getInstance().removeActivity(this.getClass());
        loadDialog = null ;
        super.onDestroy();
    }

    public void showDialog() {
        loadDialog.show();
    }

    public void showDialog(String tips) {
        loadDialog.setTips(tips);
        loadDialog.show();
        loadDialog.setTips(null);
    }


    /**
     * 可自定义标题、内容、左按钮文本、右按钮文本的提示框
     *
     * @param title      null为不修改
     * @param message    内容
     * @param btnLeftId  左按钮id，0为不修改
     * @param btnRightId 右按钮id，0为不修改
     * @param is         锁定状态，true表示允许点击窗体外和返回键关闭窗体，false表示不允许
     * @param listener   监听事件，不可为空
     */
    public void showTwoBtnDialog(String title, String message, int btnLeftId, int btnRightId, boolean is,
                                 SDialog.SDialog2Listener listener) {
        SDialog dialog = new SDialog(this, title, message, btnLeftId, btnRightId, listener);
        dialog.setCanceledOnTouchOutside(is);
        dialog.setCancelable(is);
        dialog.show();
    }


    public SDialog showTwoBtnDialog(String title, String message, int btnLeftId, int btnRightId, boolean is,
                                    SDialog.SDialog2Listener listener, boolean cancel) {
        SDialog dialog = new SDialog(this, title, message, btnLeftId, btnRightId, listener);
        dialog.setClickCancel(cancel);
        dialog.setCanceledOnTouchOutside(is);
        dialog.setCancelable(is);
        dialog.show();
        return dialog;
    }

    /**
     * loading提示
     */
    private SLoadingDialog loading;

    /**
     * 显示加载提示
     *
     * @param tips
     */
    public void showLoading(final String tips) {
        if (null == loading || loading.isWithTitle())
            loading = new SLoadingDialog(this);

        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            loading.setTips(tips);
            loading.show();
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    loading.setTips(tips);
                    loading.show();
                }
            });

        }
    }

    /**
     * @param tips
     * @param cancelable 是否可取消  false 不可以  true 可以
     */
    public void showLoading(final String tips, final boolean cancelable) {
        endLoading();
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            loading = new SLoadingDialog(this, tips, cancelable);
            loading.show();
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    loading = new SLoadingDialog(BaseActivity.this, tips, cancelable);
                    loading.show();
                }
            });
        }
    }

    /**
     * 隐藏loading
     */
    public void endLoading() {
        if (null != loading) {
            loading.dismiss();
        }
    }

    public void dismissDialog(){
        if(null != loadDialog && loadDialog.isShowing()) {
            loadDialog.dismiss();
        }
    }

    public void showToast(int resId) {
        SToast.show(resId, Gravity.CENTER);
    }
}
